import java.util.*;


public class Search {

    public static void crawl(WebGraph webGraph, String url, int graphSizeLimit) {
        int counter = 0;
        WebPage webPage = new WebPage(url);
        webGraph.addPage(webPage);

        Queue<WebPage> queue = new ArrayDeque<WebPage>();
        queue.add(webPage);

        while (counter  <= graphSizeLimit && webGraph.getSize() <= graphSizeLimit) {
            WebPage first = queue.poll();
            if (first == null)
                break;

            for (String link : first.getLinks()) {
                if (webGraph.getSize() > graphSizeLimit ||  counter > graphSizeLimit )
                    break;
                RoyalPair pair;
                WebPage newPage = new WebPage(link);
                counter++;
                if (newPage.isRoyal()) {
                    webGraph.addPage(newPage);
                    pair = new RoyalPair(first.getUrl(), newPage.getUrl());
                    Royal.tuples.add(pair);
                    queue.add(newPage);
                }
            }
        }
    }





}
